<template>
  <div class="channel-edit">
    <van-cell :border="false">
      <template #title>我的频道</template>
      <template #default>
        <van-button size="mini" @click="onEdit">{{
          isEdit ? '完成' : '编辑'
        }}</van-button>
      </template>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        v-for="(value, index) in newsList"
        :key="value.id"
        :text="value.name"
        :icon="isEdit && index !== 0 ? 'close' : ''"
        @click="remove(value, index)"
        :class="{ editItem: active === index }"
      />
    </van-grid>
    <van-cell :border="false">
      <template #title>推荐频道</template>
    </van-cell>
    <van-grid :gutter="10">
      <van-grid-item
        v-for="value in recommendChannels"
        :key="value.id"
        :text="value.name"
        @click="onAdd(value)"
      />
    </van-grid>
  </div>
</template>

<script>
import {
  getChannelListsApi,
  getRevampChannelApi,
  getRemoveChannelApi
} from '../api/home'
import { mapState } from 'vuex'
import { setItem } from '@/utils/storage.js'
export default {
  name: 'ChannelEdit',
  props: {
    newsList: {
      type: Array,
      require: true
    },
    active: {
      type: Number,
      require: true
    }
  },
  data () {
    return {
      newsLists: [], // 全部频道
      isEdit: false
    }
  },
  computed: {
    ...mapState(['user']),
    recommendChannels () {
      return this.newsLists.filter((channel) => {
        return !this.newsList.find((userChannel) => {
          return userChannel.id === channel.id
        })
      })
    }
  },
  created () {
    this.getChannelListsApi()
  },
  methods: {
    async getChannelListsApi () {
      // 全部频道列表请求
      const { data } = await getChannelListsApi()
      this.newsLists = data.data.channels
    },
    async getRevampChannelApi (val) {
      // 添加频道请求
      await getRevampChannelApi(val)
    },
    async getRemoveChannelApi (id) {
      // 删除指定的频道
      await getRemoveChannelApi(id)
    },
    onAdd (value) {
      this.newsList.push(value) // 添加频道

      if (this.user) {
        this.getRevampChannelApi({
          // 登录账号频道添加修改
          channels: [
            {
              id: value.id,
              seq: this.newsList.length
            }
          ]
        })
      } else {
        setItem('news', this.newsList) // 本地存储
      }
    },
    onEdit () {
      this.isEdit = !this.isEdit
    },
    remove (channel, index) {
      if (this.isEdit && index !== 0) {
        if (index <= this.active) {
          this.$emit('newActive', this.active - 1) // 更新频道索引
        }
        this.newsList.splice(index, 1) // 删除频道

        if (this.user) {
          this.getRemoveChannelApi(channel.id) // 登录账号频道删除修改
        } else {
          setItem('news', this.newsList) // 本地存储
        }
      } else {
        this.$emit('close', index)
      }
    }
  }
}
</script>

<style lang="less" scoped>
.channel-edit {
  padding-top: 54px;
}

/deep/.van-grid-item__icon + .van-grid-item__text {
  margin-top: 0;
}

/deep/.van-icon-close:before {
  // 修改的编辑x号图标
  content: '\e690';
  position: absolute;
  left: 30px;
  top: -25px;
  font-size: 20px;
}

/deep/.editItem {
  .van-grid-item__text {
    color: red;
  }
}
</style>
